home *** CD-ROM | disk | FTP | other *** search
- /* use msc readfile /AL; link readfile environ getenv2;
- readfile.c
-
- This program impliments a read command for batch files.
- What READFILE does is re-open the text file who's name was stored in
- the environment string, and read the next line from it. The postion in
- the file is updated and stored back in the environment string, so
- subsequent calls to READFILE will return successive lines from the
- file. The file gets opened over and over again for each line,but you
- get all the advantages of batch files with little programming.
- To use READFILE in a batch file type:
-
- READFILE <handle> <stringname>
-
- handle is the unique identifer you gave to OPENFILE.
- stringname is the name of a string to store the text in the
- environment.
-
- READFILE returns errorlevel=0 for failure, 1 for success
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <dos.h>
- #include <string.h>
-
- extern char *search_for(); /*searches environment for string*/
- extern del_string(); /*deletes a string from the environment*/
- extern int ins_string(); /*insert a new string in the environment*/
-
- char name[128]="n_"; /*space for the file name string*/
- char pos[64]="p_"; /*space for the file position string*/
- char line[200]; /*space for the line read from file*/
-
- int envseg; /*segment of environment string*/
- char far *env; /*pointer to environment string*/
- extern unsigned int far getenv2(); /* routine to find environment and its size*/
- int enl; /*length of environment in bytes*/
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int arg=1; /*where to look for arguments*/
- char *ptr; /*scratch pointer for scanning around*/
- FILE *fh; /*file handle to read with*/
- long posit; /*position in file*/
- int l; /*scratch for string lengths*/
-
- if (argc<2) /*do I have enough arguments?*/
- {
- printf("Usage: READ <handle> <pathname>\r\n");
- exit(0); /*exit if no file name*/
- }
- if (argc<3) /*if there is only one argument,*/
- arg=0; /*use it as the file name*/
- enl=getenv2(&envseg); /*get length and address of system environment*/
- FP_SEG(env)=envseg;
- FP_OFF(env)=0;
- strcat(name,argv[arg]); /*build the file name string*/
- strcat(name,"=");
- ptr=search_for(name); /*is there a file for this handle?*/
- if (ptr == NULL)
- {
- printf("You did not OPEN a file first\r\n");
- exit(0);
- }
- fh=fopen((ptr+=strlen(name)),"r"); /*open the file*/
- if (fh == NULL) /*abort if you cannot open it*/
- {
- printf("Cannot open file %s\r\n",ptr);
- exit(0);
- }
- strcat(pos,argv[arg]); /*build the file position string*/
- strcat(pos,"=");
- ptr=search_for(pos); /*find address of position string*/
- if (ptr == NULL)
- {
- printf("There is something seriously wrong in environment!\r\n");
- exit(0);
- }
- sscanf(ptr+strlen(pos),"%ld",&posit); /*get file position*/
- fseek(fh,posit,0); /*go there*/
- strcpy(line,argv[arg+1]); /*build the text string name*/
- strcat(line,"=");
- del_string(line); /*deletae any old string same name*/
- if (fgets(line+strlen(line),180,fh)== NULL) /*read a line of text*/
- exit(0); /*must have hit eof*/
- del_string(pos); /*delete the string position*/
- posit=ftell(fh); /*find out where we are in the file*/
- sprintf(pos+strlen(pos),"%ld",posit); /*now and convert to string*/
- ins_string(pos); /*store back in environment*/
- line[strlen(line)-1]=0; /*chop the newline off the end*/
- ins_string(line); /*add it to the environment*/
- fclose(fh);
- exit(1);
- }
-